package com.example.administrator.xxnapp.fragment.picture;

import android.content.Intent;
import android.support.v4.app.ActivityOptionsCompat;
import android.support.v4.util.Pair;
import android.support.v4.view.ViewCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.example.administrator.xxnapp.R;
import com.example.administrator.xxnapp.bean.PictureBean;
import com.example.administrator.xxnapp.bean.UserBean;
import com.example.administrator.xxnapp.fragment.mine.MineFragment;
import com.example.administrator.xxnapp.main.MainActivity;
import com.example.administrator.xxnapp.picture.PictureActivity;
import com.example.administrator.xxnapp.ui.EmptyDataLayout;
import com.example.administrator.xxnapp.util.Config;
import com.gyf.barlibrary.ImmersionBar;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshHeader;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.mdruby.baselibrary.BaseFragment;
import cn.mdruby.baselibrary.utils.ObjectStoreUtil;

/**
 */
public class PictureFragment extends BaseFragment implements IViewPicture,OnRefreshListener,OnLoadmoreListener,EmptyDataLayout.OnEmptyLayoutClickListener{
    @BindView(R.id.frag_picture_SmartRefreshLayout)
    SmartRefreshLayout mRefreshLayout;
    @BindView(R.id.frag_picture_RV)
    RecyclerView mRV;
    @BindView(R.id.frag_picture_EmptyDataLayout)
    EmptyDataLayout mEmptyLayout;
    protected ImmersionBar mImmersionBar;
    private PicturePresenter mPresenter;
    private List<PictureBean> mDatas;
    private RVPictureAdapter mAdapter;
    private UserBean user;

    private static PictureFragment fragment;
    public static PictureFragment getInstance(){
        if (fragment == null){
            synchronized (PictureFragment.class){
                if (fragment == null){
                    fragment = new PictureFragment();
                }
            }
        }
        return fragment;
    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_picture;
    }


    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
    }

    @Override
    public void initBeforeDatas() {
        mPresenter = new PicturePresenter(this,new PictureBean());
        mDatas = new ArrayList<>();
        user = (UserBean) ObjectStoreUtil.getObject(mContext, Config.Key.USER_OBJECT_KEY);
    }

    @Override
    public void initViews(View view) {
        ButterKnife.bind(this,view);
    }

    @Override
    public void setListeners() {
        mRefreshLayout.setOnLoadmoreListener(this);
        mRefreshLayout.setOnRefreshListener(this);

        mRefreshLayout.setEnableLoadmore(false);

        mAdapter = new RVPictureAdapter(mContext,mDatas);
        mRV.setLayoutManager(new LinearLayoutManager(mContext));
        mRV.setAdapter(mAdapter);

        mPresenter.getPicture(user.getUserId());

        mAdapter.setOnPictureItemClickListener(new RVPictureAdapter.OnPictureItemClickListener() {
            @Override
            public void onPictureItemClick(int position, View view) {
                Pair squareParticipant = new Pair<>(view, PictureActivity.PICTURE_ANIMATION);
                ActivityOptionsCompat transitionActivityOptions =
                        ActivityOptionsCompat.makeSceneTransitionAnimation(
                                getActivity(), squareParticipant);
                Intent intent = new Intent(
                        getActivity(), PictureActivity.class);
                intent.putExtra(PictureActivity.PICTURE_ANIMATION,mDatas.get(position));
                startActivity(intent, transitionActivityOptions.toBundle());
            }
        });

        mEmptyLayout.setOnEmptyLayoutClickListener(this);
    }

    @Override
    public void onRefresh(RefreshLayout refreshlayout) {
        mPresenter.getPicture(user.getUserId());
    }

    @Override
    public void onLoadmore(RefreshLayout refreshlayout) {

    }

    @Override
    public void getPictureSuccess(List<PictureBean> pictureBeans) {
        mDatas.clear();
        mDatas.addAll(pictureBeans);

        if (mRefreshLayout.getVisibility() != View.VISIBLE){
            mRefreshLayout.setVisibility(View.VISIBLE);
        }

        mEmptyLayout.setVisibility(mDatas.size() == 0?View.VISIBLE:View.GONE);
        if (mDatas.size() == 0){
            mEmptyLayout.setError(false);
            mRV.setVisibility(View.GONE);
        }else {
            mRV.setVisibility(View.VISIBLE);
            mAdapter.notifyDataSetChanged();
        }
        mRefreshLayout.finishRefresh(true);
    }

    @Override
    public void getPictureError(int resultCode, String message) {
        Toast.makeText(mContext, message, Toast.LENGTH_SHORT).show();
        mRefreshLayout.finishRefresh(false);
        mRefreshLayout.setVisibility(View.GONE);
        mEmptyLayout.setError(true);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mPresenter != null){
            mPresenter.destroy();
        }
    }

    @Override
    public void onEmptyError() {
        mPresenter.getPicture(user.getUserId());
    }

    @Override
    public void onEmptyEmpty() {

    }
}
